<?
class ManagePromotionEditForm extends Form
{
    var $hotel = array(), $room;

    function ManagePromotionEditForm()
    {
        $id 		= Url::get("id");

        if($id)
        {
            $this->hotel = Db_Promotion::getPromotionById($id);
        }

        if(!$this->hotel)
        {
            Url::redirect_current();
        }
        $this->link_css("style/paging.css");
        $this->link_css('style/jquery-ui-1.9.2.custom.css');
        $this->link_css('style/msgBoxLight.css');
        $this->link_css("style/hotel.css");
        $this->link_js('javascript/jquery/jquery-ui-1.9.2.custom.min.js');
        //
        $this->link_js("javascript/jquery/jquery.ui.core.js");
        $this->link_js("javascript/jquery/jquery.ui.widget.js");
        $this->link_js("javascript/jquery/jquery.ui.mouse.js");
        $this->link_js("javascript/jquery/jquery.ui.sortable.js");
        $this->link_js("javascript/jquery/jquery.ui.datepicker.js");
        $this->link_js("javascript/jquery/jquery.msgBox.js");
        $this->link_js("javascript/hotelform.js");
        $this->link_js("javascript/tiny_mce/jquery.tinymce.js");

    }

    function draw()
    {
        global $display;
        $display->add("WEB_ROOT",WEB_ROOT);
        $display->add("WEB_DIR",WEB_DIR);

        $this->beginForm(true);

        $act 		= Url::get("act");
        $id 		= Url::get("id");

        $hotel 		= $this->hotel;



        $display->add("hotel_id",			$hotel['id']);

        $display->add("hotel_pos",			stripslashes(Url::get('hotel_pos',$hotel['pos'])));

        $display->add('hotel_status', 			array(1=>"Hiện",2=>"Ẩn"));
        $display->add("status_id",				Url::get('hotel_status',$hotel['status']));
        $display->add("hotel_img",			    STATIC_URL.$hotel['img']);
        $display->add("hotel_name",			    $hotel['name']);
        $display->add("hotel_desc",			    $hotel['desc']);



        $display->add("title","Sửa Khuyến Mãi");
        $display->add("title_submit","Sửa");

        $display->add("message",$this->showFormErrorMessages(1));
        $display->add("success",$this->showFormSuccesMessages(1));

        $display->output("HotelForm");

        $this->endForm();
    }

    function on_submit()
    {
        $hotel['pos'] 		    = (int)Url::get('hotel_pos', 0);
        $hotel['type'] 		    = (int)Url::get('hotel_type', 1);
        $hotel['name'] 		    = Url::get('hotel_name', '');
        $hotel['desc'] 		    = Url::get('hotel_desc', '');
        mb_internal_encoding("UTF-8");
        $hotel['name'] = mb_strtoupper(mb_substr($hotel['name'],0,1)) . mb_substr($hotel['name'],1);
        $hotel['desc'] = mb_strtoupper(mb_substr($hotel['desc'],0,1)) . mb_substr($hotel['desc'],1);
        $valid = true;
        if (isset($_FILES["file_image"]) && $_FILES["file_image"]["size"] > 0) {
            $store_in = 'images/upload/banner';
            $name = md5($_FILES["file_image"]["name"]);
            move_uploaded_file($_FILES["file_image"]["tmp_name"], $store_in . $name.'.jpg');
            $hotel['img'] =  $store_in . $name.'.jpg';
        } else {
            unset($hotel['img']);
        }
        $hotel['status'] 		= (int)Url::get('hotel_status', 0);
        $hotel['created_time'] 		= time();
        $hotel['create_id'] 		= User::id();
        if($valid)
        {
            $id = Url::get('id');
            if(Db_Promotion::update($hotel,$id))//Thanh cong
            {
                $this->setFormSucces("success","Save hành công!");
                Url::redirect_current();
            }
            else
            {
                $this->setFormError("message","Có lỗi xảy ra khi thêm vào database.");
            }
        }
    }
}